package com.gobuy.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.gobuy.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Gaozexi on 2020/6/7 20:49
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
/**
 * 声明为canal的监听类
 */
@CanalEventListener
public class GoodsListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *
     * @param eventType 事件的类型
     * @param rowData
     */
    @ListenPoint(schema = "changgou_goods",table="tb_spu")
    public void goodsUp(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        System.out.println("changgou_goods中 tb_spu表数据发生了改变");
        //获取旧的数据装入Map
        Map<String,Object> oldMap = new HashMap<>(16);
        rowData.getBeforeColumnsList().forEach(column -> oldMap.put(column.getName(),column.getValue()));
        String old_marketable = (String) oldMap.get("is_marketable");
        String old_status = (String) oldMap.get("status");
        //获取新的数据装入Map
        Map<String,Object> newMap = new HashMap<>(16);
        rowData.getAfterColumnsList().forEach(column -> newMap.put(column.getName(),column.getValue()));
        String new_marketable = (String) newMap.get("is_marketable");
        String new_status = (String) newMap.get("status");

        //将变化的字段存入消息队列 TODO if条件代码健壮性判断不够
        //1.上架
        //监测 is_marketable 是否由0变1
        //整合上下架消息均发送到 is_marketable_queue,不能只发送id,应该告诉调用端 是上架还是下架操作
        if("0".equals(old_marketable)&&"1".equals(new_marketable)){
            //发送改变的sku记录的id 给rabbitMQ
            System.out.println("监测到上架的商品SPUId为" + newMap.get("id")+",发送给MQ");
            try {
                rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE,"",
                        newMap.get("id")+":UP");
            } catch (AmqpException e) {
                System.out.println("发送消息到MQ失败");
                e.printStackTrace();
            }
        }
        //TODO if条件代码健壮性判断不够
        //2.下架
        //监测 is_marketable 是否由1变0
        if("1".equals(old_marketable)&&"0".equals(new_marketable)){
            //发送改变的sku记录的id 给rabbitMQ
            System.out.println("监测到下架的商品SPUId为" + newMap.get("id")+",发送给MQ");
            try {
                rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE,"",
                        newMap.get("id")+":DOWN");
            } catch (AmqpException e) {
                System.out.println("发送消息到MQ失败");
                e.printStackTrace();
            }
        }

        //3.商品已审核
        //监测 status 是否由0变1
        if("0".equals(old_status)&&"1".equals(new_status)){
            //发送改变的sku记录的id+ADD (新增标识) 给rabbitMQ
            System.out.println("监测到SPUId=" + newMap.get("id")+"已通过审核,发送给MQ");
            try {
                rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE,"",
                        newMap.get("id")+":ADD");
            } catch (AmqpException e) {
                System.out.println("发送消息到MQ失败");
                e.printStackTrace();
            }

        }

        //4.商品未审核
        //监测 status 是否由1变0
        if("1".equals(old_status)&&"0".equals(new_status)){
            //发送改变的sku记录的id +DELETE  给rabbitMQ
            System.out.println("监测到SPUId=" + newMap.get("id")+"已撤销审核,发送给MQ");
            try {
                rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE,"",
                        newMap.get("id")+":DELETE");
            } catch (AmqpException e) {
                System.out.println("发送消息到MQ失败");
                e.printStackTrace();
            }

        }


    }

}
